package com.example.nftapp.act;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.nftapp.R;
import com.example.nftapp.http.HttpCallback;
import com.example.nftapp.http.HttpResponse;
import com.example.nftapp.http.HttpUtil;
import com.example.nftapp.utils.ToastUtil;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

public class RegisterActivity extends AppCompatActivity {

    private EditText edtPhone;
    private EditText edtPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_register);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        edtPhone = findViewById(R.id.editTextPhone);
        edtPwd = findViewById(R.id.editTextPwd);
        Button btnRegister = findViewById(R.id.btnRegister);
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = edtPhone.getText().toString();
                String password = edtPwd.getText().toString();
                HashMap<String, String> map = new HashMap<>();
                map.put("username", username);
                map.put("password", password);
                map.put("repassword", password);
                HttpUtil.doPost("https://www.wanandroid.com/user/register", map, new HttpCallback() {
                    @Override
                    public void onHttpSucceed(HttpResponse httpResponse) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtil.showToast(RegisterActivity.this, "注册成功！");
                                finish();
                            }
                        });
                    }
                });
            }
        });
    }

}